<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="keywords" content="">
  <meta name="description" content="">
  <title>登陆</title>
  <link rel="stylesheet" href="component/layui/css/layui.css">
  <link rel="stylesheet" href="admin/css/login.css">
  <link rel="stylesheet" href="admin/css/dimples.css">
  <link rel="icon" href="admin/images/favicon.ico" type="image/x-icon"/>
</head>
<!-- 代 码 结 构 -->
<body>
<div id="dimples-login" lay-title="登录">
  <div class="login-wrap">
    <div class="layui-container">
      <div class="layui-row">
        <div class="layui-col-xs12 layui-col-lg4 layui-col-lg-offset4 dimples-tc">
          <div class="layui-logo"><span><b>dimples</b> 权限系统</span></div>
        </div>
        <div class="layui-col-xs12 layui-col-lg4 layui-col-lg-offset4" id="login-div">
          <div class="layui-form" lay-filter="login-form">
            <div class="layui-anim layui-anim-upbit">
              <ul class="login-type-tab">
                <li class="active">系统登录</li>
              </ul>
              <div class="normal-login-form">
                <div class="layui-form-item">
                  <label class="layui-icon label-icon layui-icon-username"></label>
                  <input type="text" name="username" lay-verify="required"
                         placeholder="用户名" autocomplete="off" class="layui-input" value="dimples">
                </div>
                <div class="layui-form-item password-block">
                  <label class="layui-icon label-icon layui-icon-password"></label>
                  <input type="password" name="password" lay-verify="required"
                         placeholder="密码" autocomplete="off" class="layui-input" value="123456">
                </div>
                <div class="layui-form-item">
                  <div class="layui-row">
                    <div class="layui-col-xs7">
                      <label class="layui-icon label-icon layui-icon-vercode"></label>
                      <input type="text" maxlength="4" id="code" name="captcha" lay-verify="required"
                             placeholder="验证码" class="layui-input" autocomplete="off">
                    </div>
                    <div class="layui-col-xs5">
                      <img class="codeimg" id="captchaImage"/>
                    </div>
                  </div>
                </div>
                <div class="layui-form-item" style="margin-top: -10px;">
                  <input type="checkbox" name="rememberMe" title="记住我" lay-skin="primary">
                </div>
                <div class="layui-form-item">
                  <button class="layui-btn layui-btn-normal layui-btn-fluid" lay-submit
                          lay-filter="login-submit" id="login">
                    <i style="display: none"
                       class="layui-icon layui-icon-loading layui-icon layui-anim layui-anim-rotate layui-anim-loop"></i>
                    立即登录
                  </button>
                </div>
              </div>
              <div class="layui-form-item dimples-tr">
                <a id="register-href">注册账号</a>
              </div>
            </div>
          </div>
        </div>

        <!-- 注册账号 -->
        <div class="layui-col-xs12 layui-col-lg4 layui-col-lg-offset4 dimples-hide" id="register-div">
          <div class="layui-form" lay-filter="register-form">
            <div class="layui-anim layui-anim-upbit">
              <ul class="login-type-tab">
                <li class="active">账号注册</li>
              </ul>
              <div class="normal-login-form">
                <div class="layui-form-item">
                  <label class="layui-icon label-icon layui-icon-username"></label>
                  <input type="text" name="username" minlength="2" maxlength="10" lay-verify="range"
                         placeholder="用户名" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-item password-block">
                  <label class="layui-icon label-icon layui-icon-password"></label>
                  <input type="password" name="password" minlength="6" lay-verify="range"
                         placeholder="密码" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-item password-block">
                  <label class="layui-icon label-icon layui-icon-password"></label>
                  <input type="password" name="passwordB" lay-verify="required"
                         placeholder="密码确认" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-item">
                  <button class="layui-btn layui-btn-normal layui-btn-fluid" lay-submit
                          lay-filter="register-submit" id="register">
                    <i style="display: none"
                       class="layui-icon layui-icon-loading layui-icon layui-anim layui-anim-rotate layui-anim-loop"></i>
                    立即注册
                  </button>
                </div>
              </div>
            </div>
            <div class="layui-form-item dimples-tr">
              <a id="login-href">已有账号？马上登录</a>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
<script src="component/layui/layui.js"></script>
<script src="component/pear/pear.js"></script>
<script src="component/pear/dimples.js"></script>
<script>
  layui.use(['form', 'jquery', 'layer', 'button', 'popup', 'loading'], function () {
    let form = layui.form;
    let $ = layui.jquery;
    let popup = layui.popup;
    let captchaPath = BASE_URL + "/images/captcha";
    let $view = $('#dimples-login');
    let $registerDiv = $view.find('#register-div');
    let $loginDiv = $view.find('#login-div');
    let type = 'login';

    let key = initCode();

    //提交登录表单
    form.on('submit(login-submit)', function (data) {
      let loading = $(this).find('.layui-icon');
      if (loading.is(":visible")) return;
      loading.show();
      data.field.key = key;
      $.post(BASE_URL + '/form/login', data.field, function (r) {
        if (r.code == 200) {
          saveData('token', 'Bearer ' + r.token);
          saveData('user', r.user);
          saveData('perms', r.user.perms);
          popup.success(r.msg, function () {
            location.href = "index.html";
          })
        } else {
          loading.hide();
          popup.failure(r.msg, function () {
            key = initCode();
          });
        }
      });
      return false;
    });

    //提交注册表单
    form.on('submit(register-submit)', function (data) {
      if (data.field.password !== data.field.passwordB) {
        layer.msg('两次密码输入不一致！');
        return;
      }
      let loading = $(this).find('.layui-icon');
      if (loading.is(":visible")) return;
      loading.show();
      $.post(BASE_URL + '/system/user/register', data.field, function (r) {
        if (r.code == 200) {
          layer.msg('注册成功，请登录');
          loading.hide();
          $view.find('#login-href').trigger('click');
        } else {
          layer.msg(r.message);
          loading.hide();
        }
      });
      return false;
    });

    // 去注册
    $view.find('#register-href').on('click', function () {
      resetForm();
      type = 'register';
      $loginDiv.hide();
      $registerDiv.show();
    });

    // 去登陆
    $view.find('#login-href').on('click', function () {
      resetForm();
      type = 'login';
      $loginDiv.show();
      $registerDiv.hide();
    });

    // 点击更换验证码
    $view.find('#captchaImage').on('click', function () {
      initCode();
    });

    function initCode() {
      let uuid = UUID();
      document.getElementById("captchaImage").src = captchaPath + "?key=" + uuid;
      document.getElementById('code').value = "";
      return uuid;
    }

    function resetForm() {
      $registerDiv.find('input[name="username"]').val('')
          .end().find('input[name="password"]').val('')
          .end().find('input[name="passwordB"]').val('');

      $loginDiv.find('input[name="username"]').val('')
          .end().find('input[name="password"]').val('')
          .end().find('input[name="captcha"]').val('');
    }

    $(document).on('keydown', function (e) {
      if (e.keyCode === 13) {
        if (type === 'login')
          $view.find('#login').trigger("click");
        if (type === 'register')
          $view.find('#register').trigger("click");
      }
    });
  })
</script>
<script>
  if (window != top) {
    top.location.href = location.href;
  }
</script>
</body>
</html>